<template>
  <!-- 施组方案审批意见回复 -->
  <base-breadcrumb>
    <searchData
      ref="search"
      :searchData="searchArr"
      @search="search"
    ></searchData>
    <a-card>
      <list-table
        ref="table"
        class="export-table"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        :startIndex="0"
        :isShowTotal="false"
        :statisticsConfig="{ use: true, temp: statisticsTemp }"
        :export-config="{ use: false }"
        :select-config="{ use: true, type: 'checkbox', rowKey: 'id' }"
        :outSelectedRowKeys.sync="selectedRowKeys"
      >
        <div slot="prefix">
          <a-button
            slot="prefix"
            icon="download"
            type="primary"
            v-btnPermission="'tm_051_exp_01'"
            :disabled="selectedRowKeys.length === 0 || loading"
            :loading="exporting"
            @click="exportWord('select')"
          >
            选中导出
          </a-button>
          <a-button
            slot="prefix"
            icon="download"
            type="primary"
            v-btnPermission="'tm_051_exp_01'"
            :disabled="loading"
            :loading="exporting"
            @click="exportWord('all')"
          >
            全部导出
          </a-button>
        </div>
        <span
          slot="operation"
          slot-scope="text, record"
        >
          <a
            @click="handleManage(0, record)"
            v-btnPermission="'tm_051_loo_02'"
          >查看</a>
        </span>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { queryAuditApprovalByList, queryAuditApprovalByCount, exportAuditApprovalByList } from '@/api/technical/reviewReply'

const columns = [
  {
    title: '所属分公司',
    dataIndex: 'branchCompany'
  },
  {
    title: '所属项目',
    dataIndex: 'projectName'
  },
  {
    title: '施组（方案名称）',
    dataIndex: 'name'
  },
  {
    title: '方案分类',
    dataIndex: 'planTypeName'
  },
  {
    title: '版本号',
    dataIndex: 'versionNumber',
    customRender: (text) => {
      return 'V' + text
    }
  },
  {
    title: '问题数量',
    dataIndex: 'problemNum'
  },
  {
    title: '回复人',
    dataIndex: 'replyUserName'
  },
  {
    title: '回复时间',
    dataIndex: 'replyDate'
  },
  {
    title: '回复状态',
    dataIndex: 'replyStatusName',
    // customRender: (text) => {
    //   return text == 0 ? '回复' : '已回复'
    // }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    width: '170px',
    scopedSlots: { customRender: 'operation' }
  }
]
const searchArr = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect
  },
  {
    name: '方案名称',
    value: 'name',
    type: 'input'
  },
  {
    name: '回复时间',
    value: 'startEndDate',
    type: 'dateRange',
    attr: {
          rangeFormat: ['replyDateStart', 'replyDateEnd'],
        } 
  },
  {
    name: '回复人',
    value: 'replyUserName',
    type: 'input'
  },
  {
    name: '状态',
    value: 'replyStatus',
    type: 'select',
    data: [
      {
        value: 0,
        label: '待回复'
      },
      {
        value: 1,
        label: '已回复'
      }
    ]
  }
]
export default {
  name: 'a' + Date.now(),
  components: {},
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      totalInfo: {}, // 统计信息
      queryParam: {},
      exporting: false,
      // 选中列表
      selectedRowKeys: [],
      // 列表加载中
      loading: true,
      loadData: async (parameter) => {
        try {
          this.loading = true
          const param = Object.assign({ needCount: true }, parameter, { query: this.queryParam })
          queryAuditApprovalByCount(param).then((req)=>{
          if (req.code === this.SUCCESS_CODE) {
            this.totalInfo = req.data
          }
          })
          const res = await queryAuditApprovalByList(param)
          if (res.code === this.SUCCESS_CODE) {
            return { code: 200, data: res.data, ...res }
          } else {
            return { code: 200, data: [] }
          }
        } catch (error) {
          console.log(error)
          return { code: 200, data: [] }
        } finally{
          this.loading = false
        }
      }
    }
  },
  mounted() {},
  methods: {
    //搜索
    search(val = {}) {
      this.queryParam = {
        ...val
      }
      this.$refs.table.refresh(true)
    },

    // 操作栏
    handleManage(type, record) {
      this.$router.push({
        path: '/technical/ShiGroup/reviewReplyDetail',
        query: {
          type: 'view',
          // 引用的施组方案审批查看页面需要这个id
          id: record.constructionProgramId,
          // 其他信息用这个id
          infoId: record.id
        }
      })
    },
    /**
     * 统计
     */
    statisticsTemp() {
      return (
        <span>
          <span>
            问题总数：<a>{this.totalInfo.allNum || 0} </a>
          </span>
          <span>
            已回复数量：<a>{this.totalInfo.replyNum || 0} </a>
          </span>
          <span>
            | 未回复数量：<a>{this.totalInfo.noReplyNum || 0}</a>
          </span>
        </span>
      )
    },
    /**
     * @description: 选中、全部导出
     * @param {string} type leix
     * @return {void}
     */
    exportWord(type) {
      this.exporting = true
      exportAuditApprovalByList({
        needCount: true,
        query: {
          ...this.queryParam,
          ids: type === 'select' ? this.selectedRowKeys : []
        }
      })
        .then((res) => {
          console.log(res)
          // this.handleDownload(res)
        })
        .finally(() => {
          this.exporting = false
        })
    },
    /**
     * @description: 下载
     * @return {void}
     */
    handleDownload(res) {
      const blob = new Blob([res.data])
      const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = filename // 获取文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() //点击下载
      document.body.removeChild(downloadElement) //下载完成移除元素
      window.URL.revokeObjectURL(href) //释放blob对象
    }
  }
}
</script>
